class User < ActiveRecord::Base
  acts_as_user
  belongs_to :company
  has_many :tasks
  has_many :messages
  has_many :comments
  has_many :time_logs
  has_many :projects, :through =>:tasks, :order =>'projects.name'
  has_many :active_projects,:through=>:tasks, :source=>:project,:conditions=>["projects.completed = 'No' or projects.completed is NULL"],:order => 'id desc'
  has_many :completed_projects, :through => :tasks, :source=>:project,:conditions=>["completed='Yes'"],:order => 'id desc'
  has_many :managed_projects, :through => :tasks, :source=>:project,:conditions=>["tasks.role='Manager'"],:order => 'id desc'
  has_many :milestones
  has_many :logs
  has_many :writeboards
  has_many :wikis
  has_many :files, :class_name=>'Files'

  acts_as_tree :order => "username",
               :foreign_key => "report_to"

  validates_presence_of :username, :password

  def self.login(username, password,url)
    company = Company.find(:first,:conditions=>['url = ?', url])
    if username.match("@")
      find(:first,:conditions =>["email = ? and password = ? and company_id = ? and destroyed is null",username,password,company.id])
    else
      u = find(:first,:conditions =>["username=? and password =? and role = 'supper' and destroyed is null",username,password])
      if u
        u
      else
      find(:first,:conditions =>["username=? and password =? and company_id = ? and destroyed is null",username,password,company.id])
      end
    end
  end 

  def has_permission?(per)
     return true if self.role == "Account Owner"
     pers = per.split("_")
     per_i = per.split("_" + pers.last).first
     #if pers.size == 6
     #per = per.split("_" + pers.last).first if pers.size == 4
     # no permission inherit
     if NoPermissionInherit.find_by_identifier(per_i)
       return self.has_static_permission?(per)
     else
       per_parent = pers.first + "_" + pers[1] + "_" + "parent" + "_" + pers.last
       return self.has_static_permission?(per_parent,per)
     end
  end

  def have_power?(object = nil)
    if object.class == User 
      if self.id == object.id or self.administrator? or self.account_owner?
         true
      else
         false
      end
    else
      if self.administrator? or self.account_owner?
         true
      else
         false
      end 
    end
  end
 
  def try_to_login(url)
      User.login(self.username,self.password,url)
  end
  def name
      if self.last_name == "" and self.first_name == ""
	    self.username
      elsif not (self.first_name.nil? or self.first_name == "") and (self.last_name.nil? or self.last_name == "")
        self.first_name
      elsif not (self.last_name.nil? or self.last_name == "") and (self.first_name.nil? or self.first_name == "")
        self.last_name
      elsif not self.last_name.nil? and not self.first_name.nil? 
        if self.last_name.match(/[a-zA-Z]/) and self.first_name.match(/[a-zA-Z]/)
	   self.first_name.capitalize + " " + self.last_name.capitalize
        else
	   self.last_name + self.first_name
       end

      elsif self.last_name.nil? and self.first_name.nil?
        self.username
      else
        self.username
      end
  end
  
  def has_project?(id)
    projects.to_a.find{|p| p.id == id}
  end

  def projects_ids= (lists)
    tasks = self.tasks
    tasks.clear
	lists.each do |l|
	task = Task.create(:user=>self,:project => Project.find(l))
	tasks << task 
	end
	null_tasks = Task.find_by_sql "SELECT * FROM tasks WHERE user_id is NULL"
	null_tasks.each do |ntask|
	  ntask.destroy
	end
  end

  def administrator
	  return self.company.administrator
  end

  def administrator?
         if self.role == "Administrator"
	 true
	 else
	 false
	 end
  end

  def account_owner?
   if self.company.account_owner.id == self.id
	   true
	 else
	   false
   end
  end

  def supper?
   if self.role == "supper"
	   true
	 else
	   false
   end
  end

  def project_task=(pid)
	   self.tasks.each do |t|
	   if pid == t.project.id
          return t
	   end
	  end
  end
  def has_confirm_time
      
  end
  def managed_projects_total_hours(*args)
     total = 0 
       self.managed_projects.each do |p|
	      total += p.total_hours(*args)
	   end
	 return total
  end
  def total_hours(*args)
     total = 0 
       self.projects.each do |p|
	      total += p.hours(*args)
	   end
	 return total
  end
  def _managed_projects_total_hours(*args)
     total = 0 
	 if args.length == 0
       self.managed_projects.each do |p|
	      total += p.total_hours
	   end
	 else
	   if args[0] == "confirmed"
         self.managed_projects.each do |p|
	      total += p.total_hours(*args)
	     end
	   else
         self.managed_projects.each do |p|
	      total += p.total_hours(*args)
	     end
	   end
	 end
	 return total
  end
  def to_liquid
  end
end

class Manager < User
   belongs_to :administrator,
              :class_name => "Administrator",
			  :foreign_key => "report_to"
   has_many :employees,
            :class_name => "Employee",
			:foreign_key => "report_to"
end

